package com.cn.designpatterns.ex.concrete.observer.ob;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 微信服务,模拟被观察者
 *
 * @author zhangyi
 * @date 2018/12/24 18:38
 */
public class WeChat implements ObserverableService {

    private List<ObserverService> list;
    private String message;

    public WeChat() {
        list = new ArrayList<>();
    }

    @Override
    public void register(ObserverService observerService) {
        list.add(observerService);
    }

    @Override
    public void remove(ObserverService observerService) {
        if(Objects.nonNull(observerService)) {
            list.remove(observerService);
        }
    }

    @Override
    public void notifyall() {
        for(ObserverService o : list){
            o.update(message);
        }
    }

    public void setInformation(String message){
        this.message = message;
        System.out.println("微信消息更新了: "+message);
        notifyall();
    }
}
